/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aeronef.builder;

import aeronef.appStates.GameState;
import aeronef.model.Gun;
import com.jme3.asset.TextureKey;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.shapes.SphereCollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.material.Material;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Sphere;
import com.jme3.texture.Texture;

/**
 *
 * @author Hagan
 */ 
public class ProjectileBuilder extends AbstractBuilder {
    private RigidBodyControl ball_phy; 

    
    public ProjectileBuilder(GameState gameState) {
        super(gameState);
    }

    public void buildFromDefinition(Gun gun) {
        buildProjectileGeometry(gun);
    }
    
    private void buildProjectileGeometry(Gun gun) {
        
        BulletAppState bulletAppState = stateManager.getState(BulletAppState.class);
        
        Sphere sphere = new Sphere(10, 10, 0.05f);
        Material stone_mat;
        stone_mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        TextureKey key2 = new TextureKey("Textures/seamless_rock2.png");
        key2.setGenerateMips(true);
        Texture tex2 = assetManager.loadTexture(key2);
        stone_mat.setTexture("ColorMap", tex2);
        Geometry ball_geo = new Geometry("cannon ball", sphere);
        ball_geo.setMaterial(stone_mat);
        gun.attachChild(ball_geo);
        /** Position the cannon ball  */
        ball_geo.setLocalTranslation(Vector3f.UNIT_Z.mult(0.5f));
        /** Make the ball physcial with a mass > 0.0f */
        ball_phy = new RigidBodyControl(0.01f);
        /** Add physical ball to physics space. */
        ball_geo.addControl(ball_phy);
        bulletAppState.getPhysicsSpace().add(ball_phy);
        /** Accelerate the physcial ball to shoot it. */
        ball_phy.setLinearVelocity(gun.getWorldRotation().mult(new Vector3f(0, 0, 100)));
        
        //gun.attachChild(ball_geo);
    }

}
